package com.health.service.impl;

import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.log4j.Logger;

import com.health.model.User;
import com.health.service.UserService;
import com.health.util.PKGenerator;

public class UserServiceImpl implements UserService {
	
	private static SqlSessionFactory factory;
	private SqlSession session = null;
	
	static Log log = LogFactory.getLog(UserServiceImpl.class);
	static Logger logger = Logger.getLogger(UserServiceImpl.class);
	static {
		String resource = "sqlMapConfig.xml";
		try {
			Reader reader = Resources.getResourceAsReader(resource);
			factory = new SqlSessionFactoryBuilder().build(reader);
			reader.close();

		} catch (IOException e) {
			log.error(e.getCause());
		}
	}
	
	@Override
	public int insert(User u) {
		session = factory.openSession();
		UserService service = session.getMapper(UserService.class);
		
		String id = PKGenerator.userIdGenerator(u);
		u.setId(id);
		int flag = service.insert(u);				//1成功 0失败
		session.commit();								
		if(flag==1) {
			log.info("--INSERT USER SUCCESS");
			log.info(u.toString());
		} else {
			log.info("==INSERT USER FAIL== flag = " + flag);
		}
		session.close();
		return flag;
	}

	@Override
	public int deleteByPK(String id) {
		session = factory.openSession();
		UserService service = session.getMapper(UserService.class);
		int flag = service.deleteByPK(id);
		session.commit();
		if(flag == 1) {
			log.info("---- DELETE USER SUCCESS ----" + id);
		} else {
			log.info("---- DELETE USER FAILED ----" + id);
		}
		session.close();
		return flag;
	}

	@Override
	public int deleteByPK(User u) {
		return 0;
	}

	@Override
	public int updateByPK(User u) {
		session = factory.openSession();
		UserService service = session.getMapper(UserService.class);
		int flag = service.updateByPK(u);
		session.commit();
		session.close();
		return flag;
	}

	@Override
	public User selectByPK(String id) {
		session = factory.openSession();
		UserService service = session.getMapper(UserService.class);
		User user = new User();
		user = service.selectByPK(id);
		log.info("----USER DETAIL(UserService)---- " + user);
		session.close();
		return user;
	}

	@Override
	public List<User> selectList() {
		session = factory.openSession();
		UserService service = session.getMapper(UserService.class);
		List<User> list = new ArrayList<User>();
		list = service.selectList();
		log.info("---- selectList(UserService) ---- " + list.size());
		session.close();
		return list;
	}

	

	@Override
	public List<User> selectForList(User searchVo) {
		session = factory.openSession();
		UserService service = session.getMapper(UserService.class);
		
		List<User> list = new ArrayList<User>();
		list = service.selectForList(searchVo);
		log.info("---- selectForList(UserService) ----" + list.size());
		session.close();
		return list;
	}

	


	@Override
	public int canLogin(User user) {
		session = factory.openSession();
		UserService service = session.getMapper(UserService.class);
		
		int flag = service.canLogin(user);		//return 1:login success 0:login fail
		if(flag == 1){
			log.info("----- login succeed -----" + user.getEmail());
		} else {
			log.info("----- login failed(logggg) BY LOG-----");
			logger.info("----- login failed(loggerrrrr) BY LOGGER-----");
		}
		return flag;
	}
	
	
	
}
